package com.sw.bbs.board.controller;

import com.github.pagehelper.PageInfo;
import com.sw.bbs.common.common.GlobalException;
import com.sw.bbs.board.service.CommentService;
import com.sw.bbs.board.vo.request.comment.InsertCommentRequest;
import com.sw.bbs.board.vo.request.comment.QueryCommentRequest;
import com.sw.bbs.board.vo.request.comment.UpdateCommentRequest;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(value = "评论模块", description = "评论模块", tags = {"Comment Module"})
@RestController()
@RequestMapping("/comment")
@Slf4j
public class CommentController {


    @Autowired
    private CommentService commentService;

    @GetMapping("/list")
    @ApiOperation(value = "分页查询评论接口", notes = "分页查询评论帖子", response = PageInfo.class)
    @ApiResponses({@ApiResponse(code = 200, message = "分页结果结构", response = PageInfo.class),
            @ApiResponse(code = 201, message = "列表详情", response = PageInfo.class)})
    public ResponseEntity<?> queryCommentList(@ApiParam @ModelAttribute QueryCommentRequest queryRequest)
            throws GlobalException {
        try {
            PageInfo pageResponse =  commentService.queryCommentList(queryRequest);
            return new ResponseEntity<>(pageResponse, HttpStatus.OK);
        } catch (Exception e) {
            log.error("queryCommentList", e);
            throw new GlobalException("/comment/queryCommentList", e.getMessage());
        }
    }


    @PostMapping("/insert")
    @ApiOperation(value = "插入帖子评论接口", notes = "插入帖子评论", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 201, message = "插入帖子评论", response = ResponseEntity.class)})
    public ResponseEntity<?> insertComment(@ApiParam @ModelAttribute InsertCommentRequest insertCommentRequest,
                                               HttpServletRequest httpServletRequest)
            throws GlobalException {
        try {
            Integer result = commentService.insert(insertCommentRequest, httpServletRequest);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            log.error("insertComment", e);
            throw new GlobalException("/comment/insertComment", e.getMessage());
        }
    }

    @PostMapping("/edit")
    @ApiOperation(value = "编辑帖子评论接口", notes = "编辑评论", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 201, message = "编辑评论", response = ResponseEntity.class)})
    public ResponseEntity<?> editComment(@ApiParam @ModelAttribute UpdateCommentRequest updateCommentRequest,
                                               HttpServletRequest httpServletRequest)
            throws GlobalException {
        try {
            Integer result = commentService.edit(updateCommentRequest, httpServletRequest);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            log.error("editComment", e);
            throw new GlobalException("/comment/editComment", e.getMessage());
        }
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除评论接口", notes = "删除评论", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 201, message = "删除评论", response = ResponseEntity.class)})
    public ResponseEntity<?> deleteComment(@ApiParam @ModelAttribute Integer commentId,
                                               HttpServletRequest httpServletRequest)
            throws GlobalException {
        try {
            Integer result = commentService.delete(commentId, httpServletRequest);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            log.error("deleteComment", e);
            throw new GlobalException("/comment/deleteComment", e.getMessage());
        }
    }


}
